<template>
  <div style="height: 100%; width: 100%">
    <div class="_bg">
      <div class="_title">嘉兴第一医院管理平台</div>
      <div class="login_logo">
        <Lottie :animation-data="Animation" :loop="true" :autoplay="true" />
      </div>
      <div class="login_Box">
        <div class="loginBox_title">
          <span>欢迎登录</span>
          <div class="title_bot">
            <div></div>
            <p>welcome to login</p>
            <div></div>
          </div>
        </div>
        <div class="loginBox_form">
          <div class="_zhanghao">
            <i style="background-image: url('/imgs/login/zh_icon.png')"></i>
            <input v-model="adminValue" type="text" placeholder="请输入用户名" />
          </div>
          <div>
            <i style="background-image: url('/imgs/login/mm_icon.png')"></i>
            <input v-model="passwordValue" type="password" placeholder="请输入密码" />
          </div>
          <span style="color: red;height: 10px;">{{ msgStrting }}</span>
          <span class="btnLogin" @click="loginAdd">登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import Lottie from '@/components/lottie.vue'
import Animation from '@/assets/lottie/data.json'
import router from '@/router'
import { login } from '@/api/login.js'
import Cookie from 'js-cookie'
const adminValue = ref('')
const passwordValue = ref('')
//登录提示信息
const msgStrting = ref('')
const loginAdd = async () => {
  if (!adminValue.value && !passwordValue.value) {
    // ElMessage.error('账户名密码不能为空');
    msgStrting.value = '账户名密码不能为空'
    return;  // 直接返回，阻止后续逻辑执行
  }
  // 账户名或密码为空时提示错误
  if (!adminValue.value) {
    // ElMessage.error('账户名不能为空');
    msgStrting.value = '账户名不能为空'
    return;  // 直接返回，阻止后续逻辑执行
  }
  if (!passwordValue.value) {
    // ElMessage.error('密码不能为空');
    msgStrting.value = '密码不能为空'
    return;  // 直接返回，阻止后续逻辑执行
  }
  let dat = {
    username: adminValue.value,
    password: passwordValue.value
  }
  let res = await login(dat);
  if (res.code == 200) {
    ElMessage.success('登录成功！')
    localStorage.setItem('token', res.token)
    Cookie.set('Admin-Token', res.token)
    router.push('/gateway')
  } else {
    msgStrting.value = res.msg
    // ElMessage.error(res.msg)
  }
  refresh()
}
const refresh = () => {
  setTimeout(
    () => {
      localStorage.removeItem("token")
      Cookie.remove("Admin-Token")
      router.push('/login')
    },
    //半小时
    // 1000 * 60 * 60
    //改为4小时
    4 * 3600 * 1000
  )
}
</script>

<style lang="scss" scoped>
._bg {
  background-image: url('/imgs/login/loginBg.png');
  background-size: 100% 100%;
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;
  background-position: center;
  position: relative;
  // z-index: 9999;
}

._title {
  position: absolute;
  top: 196px;
  left: 211px;
  font-family: ShiShangZhongHeiJianTi;
  font-weight: 400;
  font-size: 50px;
  color: #4f9df4;
}

.login_logo {
  width: 805px;
  height: 705px;
  // background-image: url('/imgs/login/login_logo.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  position: absolute;
  top: 296px;
  left: 155px;
}

.login_Box {
  width: 526px;
  height: 488px;
  background-image: url('/imgs/login/boxBg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center;
  position: absolute;
  top: 23%;
  right: 16%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .loginBox_title {
    width: 382px;
    height: 70px;
    margin-top: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    span {
      font-family: ShiShangZhongHeiJianTi;
      font-weight: 400;
      font-size: 35px;
      color: #4f9df4;
    }

    .title_bot {
      margin-top: 16px;
      display: flex;
      align-items: center;

      div {
        width: 94px;
        height: 2px;
        border: 1px solid #4f9df4;
      }

      p {
        font-family: ShiShangZhongHeiJianTi;
        font-weight: 400;
        font-size: 22px;
        color: #4f9df4;
        margin: 0 10px;
        white-space: nowrap;
      }
    }
  }

  .loginBox_form {
    margin-top: 50px;
    width: 410px;
    height: 260px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;

    div {
      width: 406px;
      height: 48px;
      background: #ffffff;
      border-radius: 8px;
      border: 1px solid #54b5ff;
      display: flex;
      align-items: center;
      padding: 0 20px;

      i {
        background-size: 100% 100%;
        background-repeat: no-repeat;
        background-position: center;
        width: 27px;
        height: 29px;
      }

      input {
        margin-left: 12px;
        border: none;
        background: none;
        height: 46px;
        width: 270px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        color: #545454;
      }

      input::placeholder {
        font-family: Microsoft YaHei;
        font-weight: 400;
        font-size: 20px;
        color: #c0c0c0;
        line-height: 17px;
        font-style: italic;
      }
    }

    .btnLogin {
      width: 405px;
      height: 60px;
      background: #4f9df4;
      border-radius: 8px;
      font-family: ShiShangZhongHeiJianTi;
      font-weight: 400;
      font-size: 28px;
      color: #ffffff;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    // span {
    //   width: 405px;
    //   height: 60px;
    //   background: #4f9df4;
    //   border-radius: 8px;
    //   font-family: ShiShangZhongHeiJianTi;
    //   font-weight: 400;
    //   font-size: 28px;
    //   color: #ffffff;
    //   display: flex;
    //   justify-content: center;
    //   align-items: center;
    // }
  }
}
</style>
